package com.hp.controller;

import com.github.pagehelper.PageInfo;
import com.hp.bean.Orders;
import com.hp.bean.OrdersExample;
import com.hp.service.OrdersService;
import com.hp.util.JsonData;
import com.hp.util.RedisUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Resource
    private OrdersService service;
    @Resource
    RedisUtil redisUtil;

    //用户手机端页面查看优惠价页面
    @GetMapping
    public String coupon(){
        return "/view/customerPhone/html/order.html";
    }

    @RequestMapping("/selectListByPhone")
    @ResponseBody
    public Object list(HttpSession session) {
        if(session.getAttribute("phone")!=null){
            Orders orders = new Orders();
            orders.setPhone((String)session.getAttribute("phone"));
            List<Orders> list = service.selectOrdersList(orders);
            return JsonData.buildSuccess(list);
        }
        return  JsonData.buildError("请登录后在查看订单信息");

    }

    @RequestMapping("/find")
    @ResponseBody
    public Object find(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.createCriteria();

        //未具体实现分页的写法


        PageInfo<Orders> list =service.pageFind(example, page, limit);
        return JsonData.buildSuccess(list);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer id) {
        Orders orders = service.findByid(id);
        return JsonData.buildSuccess(orders);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(Orders orders) {
        int i = service.upp(orders);
        return i==1? JsonData.buildSuccess("修改成功！"): JsonData.buildError("修改失败");

    }

    /**
     * 修改
     */
    @RequestMapping("/orderEnd")
    @ResponseBody
    public Object orderEnd(Orders obj,HttpSession session) {
        obj.setPhone((String) session.getAttribute("phone"));
        int i = service.orderEnd(obj);
        return JsonData.buildSuccess(i > 0 ? "修改成功！" : "修改失败！");
    }
    /**
     *  派单or解除派单
     * @param obj   订单信息
     * @param type  1是解除派单  2是派单
     * @return
     */
    @RequestMapping("/sendMaster")
    @ResponseBody
    public Object sendMaster(Orders obj,HttpSession session,@RequestParam(value = "type",defaultValue = "1")Integer type) {
        if(session.getAttribute("mid")!=null&&!((session.getAttribute("mid")+"").equals(""))){//工程师自己接单情况
            obj.setMid(session.getAttribute("mid")+"");//如果有mid证明这个账号是工程师账号
        }
        int i = service.sendMaster(obj,type);
        if(type==1){
            return JsonData.buildSuccess(i > 0 ? "解除派单成功！" : "解除派单失败！");
        }else{
            return JsonData.buildSuccess(i > 0 ? "派单成功！" : "派单失败！");
        }
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Orders obj) {
        Orders orders = service.selectOrdersByPhone(obj.getPhone());
        System.out.println(orders);
        if(orders!=null){
            return JsonData.buildError("该手机号已经存在未结束的订单！");
        }else{
            int i = service.insertOrders(obj);
            return JsonData.buildSuccess(i > 0 ? "下单成功！" : "下单失败！");
        }
    }
    @RequestMapping("/add2")
    @ResponseBody
    public Object add2(Orders obj,HttpSession session) {
        obj.setPhone((String) session.getAttribute("phone"));
        Orders orders = service.selectOrdersByPhone(obj.getPhone());
        System.out.println(orders);
        if(orders!=null){
            return JsonData.buildError("该手机号已经存在未结束的订单！");
        }else{
            int i = service.insertOrders(obj);
            return JsonData.buildSuccess(i > 0 ? "下单成功！" : "下单失败！");
        }
    }
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer id) {
        int i = service.del(id);
        return i==1? JsonData.buildSuccess("删除成功！"): JsonData.buildError("删除失败");

    }

}
